#使用llamaindex创建一个agent
import asyncio
from llama_index.core.agent.workflow import ReActAgent
from llama_index.core.workflow import Context
from llama_index.llms.langchain import LangChainLLM
from langchain_community.chat_models import ChatTongyi
from llama_index.core.memory.chat_memory_buffer import ChatMemoryBuffer
from llama_index.core import VectorStoreIndex, SimpleKeywordTableIndex, Settings, SimpleDirectoryReader
from llama_index.embeddings.dashscope import (
    DashScopeEmbedding,
    DashScopeTextEmbeddingModels,
    DashScopeTextEmbeddingType
)

#词嵌入模型
Settings.embed_model = DashScopeEmbedding(
    model_name=DashScopeTextEmbeddingModels.TEXT_EMBEDDING_V3,
    text_type=DashScopeTextEmbeddingType.TEXT_TYPE_DOCUMENT,
    api_key="sk-f97e3654139742a4b01a99631628d36d"
)

# 初始化LLM
Settings.llm = LangChainLLM(
    ChatTongyi(model="qwen-plus", api_key="sk-f97e3654139742a4b01a99631628d36d")
)

#加载文档
documents = SimpleDirectoryReader(r"D:\Code\sshcode\llamaindex").load_data()
#创建索引
index = VectorStoreIndex.from_documents(documents)
#创建查询虚拟引擎
query_engine = index.as_query_engine()
res = query_engine.query("怎么退款?")
print(res)
